/**
 * Copyright by e-shion.net
 */
package net.xfok.ui.dock;

import net.xfok.ui.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author 2011-1-18 Howard
 * 
 */
public class Dock2 extends Activity {
	private static final String TAG = "Dock";
	private static final int NOR_SIZE = 45;
	private static final int MID_SIZE = (int) (NOR_SIZE * 1.2);
	private static final int BIG_SIZE = (int) (NOR_SIZE * 1.5);
	private static final int DOCK_START=270;
	private static final int DOCK_END=540;
	
	private static final int FOCUS_HOME=4;
	private static final int FOCUS_MEDIA=3;
	private static final int FOCUS_EBOOK=2;
	private static final int FOCUS_MSG=1;
	private static final int FOCUS_BROWSER=0;
	
	private ImageView btHome, btMedia, btEbook, btMsg, btBrowser;
	private LinearLayout mDock;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dock);
		setViews();
	}

	private void setViews() {
		btHome = (ImageView) findViewById(R.id.bt_home);
		btHome.setBackgroundColor(Color.TRANSPARENT);
		btHome.setOnClickListener(new DockClick());
		btMedia = (ImageView) findViewById(R.id.bt_media);
		btMedia.setBackgroundColor(Color.TRANSPARENT);
		btMedia.setOnClickListener(new DockClick());
		btEbook = (ImageView) findViewById(R.id.bt_ebook);
		btEbook.setBackgroundColor(Color.TRANSPARENT);
		btEbook.setOnClickListener(new DockClick());
		btMsg = (ImageView) findViewById(R.id.bt_msg);
		btMsg.setBackgroundColor(Color.TRANSPARENT);
		btMsg.setOnClickListener(new DockClick());
		btBrowser = (ImageView) findViewById(R.id.bt_browser);
		btBrowser.setBackgroundColor(Color.TRANSPARENT);
		btBrowser.setOnClickListener(new DockClick());
		mDock=(LinearLayout)findViewById(R.id.dock);
		mDock.setOnTouchListener(new DockTouch());
		focus(FOCUS_HOME);
	}

	private void toBig(View view) {
		setSize(view, BIG_SIZE);
	}

	private void toMid(View view) {
		setSize(view, MID_SIZE);
	}

	private void toNor(View view) {
		setSize(view, NOR_SIZE);
	}

	private void setSize(View view, int size) {
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				size, size);
		view.setLayoutParams(layoutParams);
	}

	private void focus(int n){
		switch (n) {
		case FOCUS_HOME:
			toBig(btHome);
			toMid(btMedia);
			toNor(btEbook);
			toNor(btMsg);
			toNor(btBrowser);
			break;
		case FOCUS_MEDIA:
			toMid(btHome);
			toBig(btMedia);
			toMid(btEbook);
			toNor(btMsg);
			toNor(btBrowser);
			break;
		case FOCUS_EBOOK:
			toNor(btHome);
			toMid(btMedia);
			toBig(btEbook);
			toMid(btMsg);
			toNor(btBrowser);
			break;
		case FOCUS_MSG:
			toNor(btHome);
			toNor(btMedia);
			toMid(btEbook);
			toBig(btMsg);
			toMid(btBrowser);
			break;
		case FOCUS_BROWSER:
			toNor(btHome);
			toNor(btMedia);
			toNor(btEbook);
			toMid(btMsg);
			toBig(btBrowser);
			break;
		default:
			break;
		}
	}
	private class DockTouch implements OnTouchListener{
		public boolean onTouch(View v, MotionEvent event) {
			float x=event.getX();
			float y=event.getY();
			if(y>0&&x>DOCK_START&&x<DOCK_END){
				int n=(int)(x-DOCK_START)/NOR_SIZE;
				focus(n);
			}	
			return true;
		}		
	}
	private class DockClick implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.bt_home:
				focus(FOCUS_HOME);
				break;
			case R.id.bt_media:
				focus(FOCUS_MEDIA);
				break;
			case R.id.bt_ebook:
				focus(FOCUS_EBOOK);
				break;
			case R.id.bt_msg:
				focus(FOCUS_MSG);
				break;
			case R.id.bt_browser:
				focus(FOCUS_BROWSER);
				break;
			default:
				break;
			}
		}
		
	}
}
